package com.ycode.bang.anbang.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.adapter.SelectPeopleAdapter;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 选择分配人员
 */
public class SelectPeopleActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.lv_bm)
    ListView mLvbm;
    @InjectView(R.id.lv_ry)
    ListView mLvry;
    @InjectView(R.id.back)
    ImageView back;//返回键
    @InjectView(R.id.tv_ok)
    TextView mTvOk;//分配人员后的确定按钮


    BmAdapter bmAdapter;
    SelectPeopleAdapter spAdapter;
    List<DepartmentModel> list = new ArrayList<DepartmentModel>();//部门的数据
    List<DepartmentModel> peopleList = new ArrayList<DepartmentModel>();//部门下人的集合
    String PeopleId = "";//这个是被分配人的名字
    String orderId = "";//
    String buMengId = "";//
    int tag;
    private String INTERFACE_PATH = "";//接口路径//获取详情的
    private String contect = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_people);
        ButterKnife.inject(this);
        if (this.getIntent().getExtras() != null) {
            Bundle bundle = this.getIntent().getExtras();
            orderId = bundle.getInt("orderId") + "";
            tag = bundle.getInt("tag");
            if (!TextUtils.isEmpty(bundle.getString("contect"))) {
                contect = bundle.getString("contect");
            }
        }
        initView();
    }

    public void initView() {
        back.setOnClickListener(this);
        mTvOk.setOnClickListener(this);
        if (tag == 1||tag==99) {
            //add by wss sure chengwei  99是再次发起投诉的人员是公共
            INTERFACE_PATH = RequestMapDev.AssignmentOrder;
        } else if (tag == 2) {
            INTERFACE_PATH = RequestMapDev.AssignmentOrder2;
        } else if (tag == 3) {
            INTERFACE_PATH = RequestMapDev.AssignmentOrder3;
        }
        GetDepartment();

//        for (int i = 0; i < 4; i++) {
//            if (i == 0) {
//                list.add(new SelectPeopleModel(i + "", true));
//            } else {
//                list.add(new SelectPeopleModel(i + "", false));
//            }
//
//        }


//        for (int i = 0; i < 8; i++) {
//            peopleList.add(new SelectPeopleModel(i + "", false));
//        }
//        spAdapter = new SelectPeopleAdapter(this, peopleList);
//        mLvry.setAdapter(spAdapter);

        mLvbm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).isCheck = false;
                }
                list.get(position).isCheck = true;
                bmAdapter.notifyDataSetChanged();
                peopleList.clear();
                getPeople(list.get(position).getValue() + "");
                buMengId = list.get(position).getValue() + "";
//                spAdapter.notifyDataSetChanged();
                PeopleId = "";
            }
        });
        mLvry.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                name = peopleList.get(position).getName();
                PeopleId = peopleList.get(position).getValue() + "";
                for (int i = 0; i < peopleList.size(); i++) {
                    peopleList.get(i).isCheck = false;
                }
                peopleList.get(position).isCheck = true;
                spAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(PeopleId)) {
                    ToastUtil.show(this, "请选择分配人员！");
                    break;
                }
                 if (!TextUtils.isEmpty(contect)) {
                    if (tag == 1) {
                        INTERFACE_PATH = RequestMapDev.AssignmentOrder11;
                    } else if (tag==99){
                        INTERFACE_PATH = RequestMapDev.AssignmentOrder13;//特殊处理
                    }else{
                        INTERFACE_PATH = RequestMapDev.AssignmentOrder13;
                    }
                }
                assignmentOrder();
                break;
        }
    }

    /**
     * 分配工单下部门的适配器
     */
    public class BmAdapter extends ArrayAdapter<DepartmentModel> {
        private Context context;
        private LayoutInflater layoutInflater;
        public List<DepartmentModel> list;

        public BmAdapter(Context context, List<DepartmentModel> list) {
            super(context, 0, list);
            this.context = context;
            this.list = list;
            layoutInflater = LayoutInflater.from(context);

        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Holder holder;
            if (null == convertView) {
                convertView = layoutInflater.inflate(R.layout.item_bm, null);
                ButterKnife.inject(this, convertView);
                holder = new Holder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.mTvbm.setText(list.get(position).getAlias());
            if (list.get(position).isCheck) {
                holder.mTvbm.setTextColor(getResources().getColor(R.color.blue));
                holder.mLibm.setBackgroundResource(R.color.white);
            } else {
                holder.mTvbm.setTextColor(getResources().getColor(R.color.black));
                holder.mLibm.setBackgroundResource(R.color.lightgray3);
            }
           /* holder.mLibm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });*/
            return convertView;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        class Holder {
            Holder(View view) {
                ButterKnife.inject(this, view);
            }

            @InjectView(R.id.tv_bm)
            public TextView mTvbm;
            @InjectView(R.id.lin_bm)
            public LinearLayout mLibm;

        }

    }

    /**
     * 获取部门
     */
    private void GetDepartment() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        FinalHttpUtil.get(RequestMapDev.GetDepartment, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        list.clear();
                        list = gson.fromJson(data, new TypeToken<List<DepartmentModel>>() {
                        }.getType());
                        bmAdapter = new BmAdapter(SelectPeopleActivity.this, list);
                        mLvbm.setAdapter(bmAdapter);
                    } else {
                        ToastUtil.show(SelectPeopleActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(SelectPeopleActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
                ToastUtil.show(SelectPeopleActivity.this, strMsg);
            }
        });
    }

    /**
     * 确认分配
     */
    private void getPeople(String id) {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        mAjaxParam.put("id", id);
        FinalHttpUtil.get(RequestMapDev.GetPeople, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        peopleList.clear();
                        peopleList = gson.fromJson(data, new TypeToken<List<DepartmentModel>>() {
                        }.getType());
                        spAdapter = new SelectPeopleAdapter(SelectPeopleActivity.this, peopleList);
                        mLvry.setAdapter(spAdapter);
                    } else {
                        ToastUtil.show(SelectPeopleActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(SelectPeopleActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
                ToastUtil.show(SelectPeopleActivity.this, strMsg);
            }
        });
    }

    /**
     * 分配人员
     */
    private void assignmentOrder() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        mAjaxParam.put("empId", PeopleId);
        mAjaxParam.put("orderId", orderId);
        mAjaxParam.put("did", buMengId);
        if (!TextUtils.isEmpty(contect)) {
            mAjaxParam.put("reprocessReason", contect);
        }
        FinalHttpUtil.post(INTERFACE_PATH, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        ToastUtil.show(SelectPeopleActivity.this, "分配成功！");
                        finish();
                    } else {
                        ToastUtil.show(SelectPeopleActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(SelectPeopleActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
                ToastUtil.show(SelectPeopleActivity.this, strMsg);
            }
        });
    }
}
